﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PlanningGIS.Win.Controls
{
   
    public partial class DragDropTreeView : TreeView
    {
        /// <summary>
        /// 
        /// </summary>
        public DragDropEffects pDragDropEffects;

        /// <summary>
        /// 
        /// </summary>
        public DragDropTreeView()
        {
            InitializeComponent();
        }

        #region Inherite TreeView

        /// <summary>
        /// 选择要素
        /// </summary>
        /// <param name="e"></param>
        protected override void OnItemDrag(ItemDragEventArgs e)
        {
            base.OnItemDrag(e);
            pDragDropEffects = DragDropEffects.Move;
            DoDragDrop(e.Item, pDragDropEffects);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDragLeave(EventArgs e)
        {
            base.OnDragLeave(e);
            pDragDropEffects = DragDropEffects.None;
        }

        /// <summary>
        /// 控件内部移动
        /// </summary>
        /// <param name="drgevent"></param>
        protected override void OnDragOver(DragEventArgs drgevent)
        {
            base.OnDragOver(drgevent);
            if (drgevent.Data.GetDataPresent("System.Windows.Forms.TreeNode", true) == false)
                return;
            Point pPoint = new Point(drgevent.X, drgevent.Y);
            TreeNode pTreeNode = GetNodeAt(pPoint);
            if (!(SelectedNode is TreeNode))
            {
                SelectedNode = pTreeNode;
                TreeNode pDropNode = drgevent.Data.GetData("System.Windows.Forms.TreeNode") as TreeNode;
                while (pTreeNode == null)
                {
                    if (pDropNode is TreeNode)
                    {
                        drgevent.Effect = DragDropEffects.None;
                        return;
                    }
                    pDropNode = pDropNode.Parent;
                }
            }
            drgevent.Effect = DragDropEffects.Move;
        }
        #endregion

    }
}
